package com.sykj.manage.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.manage.service.BisShopUserService;
import com.sykj.manage.model.BisShop;
import com.sykj.manage.model.BisShopUser;
import com.sykj.util.ManageBody;
import com.sykj.util.PageUtil;

/**
 * 店家信息表控制类
 * @author 蔡聪
 *	2018年9月11日下午5:24:46
 */
@Controller
@RequestMapping("/bisshopuser")
public class BisShopUserController {
	
	/**
	 * 调用店家信息业务层
	 */
	@Autowired
	BisShopUserService bisshopuserservice;
	
	/**
	 * 显示全部店主信息实现方法
	 * @param model
	 * @param bisShopUser
	 * @return
	 */
	@RequestMapping("/")
	@ResponseBody
	public ManageBody bisshopuserShow(Model model,BisShopUser bisShopUser,Integer offset,Integer pageSize){
		PageHelper.startPage(offset==null?1:PageUtil.getPageNum(offset, pageSize),pageSize==null?10:pageSize,true);
		List<BisShopUser> allBisShopUser = bisshopuserservice.getAllBisShopUser(bisShopUser);
		PageInfo<BisShopUser>pageInfo=new PageInfo<BisShopUser>(allBisShopUser);
		return ManageBody.getData((int)pageInfo.getTotal(),pageInfo.getList(),"");
	}
	
	/**
	 * 根据店主id来实现删除方法
	 * @param shop_user_id
	 * @return
	 */
	@RequestMapping("/del/{shop_user_id}")
	@ResponseBody
	public ManageBody del(@PathVariable("shop_user_id")String shop_user_id){
		int deleBisShop = bisshopuserservice.deleBisShop(shop_user_id);
		if (deleBisShop>0) {
			return ManageBody.getData(1, deleBisShop, "");
		}else {
			return ManageBody.getData(1, deleBisShop, "");
		}
		
		
	}
	//添加
	@RequestMapping("/adds")
	public String adds(BisShopUser bisShopUser){
		String password = bisShopUser.getPassword();
		bisShopUser.setPassword(DigestUtils.md5Hex(password));
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhhmmss");
		bisShopUser.setShop_user_id(format.format(date));
		bisShopUser.setCreate_time(new Date());
		bisshopuserservice.insertBisShopUser(bisShopUser);
		return "redirect:/bisshopuser/";
		
	}
	
	//修改前查出数据
	@RequestMapping("/updateAll")
	@ResponseBody
	public Object updateAll(String shop_user_id){
		BisShopUser updateAll = bisshopuserservice.updateAll(shop_user_id);
		return updateAll;
	}
	
	//修改
	@RequestMapping("/updateId")
	@ResponseBody
	public ManageBody updateId(BisShopUser bisShopUser){
		bisShopUser.setModify_time(new Date());
		int updateBisShopUser = bisshopuserservice.updateBisShopUser(bisShopUser);
		if (updateBisShopUser>0) {
			return ManageBody.getData(1, updateBisShopUser, "");
		}else{
			return ManageBody.getData(1, updateBisShopUser, "");
		}
	}
	
	//重置密码
	@RequestMapping("/updapassword")
	@ResponseBody
	public ManageBody updapassword(BisShopUser bisShopUser){
		String password = bisShopUser.getPassword();
		bisShopUser.setPassword(DigestUtils.md5Hex(password));
		bisShopUser.setModify_time(new Date());
		int updatePassword = bisshopuserservice.updatePassword(bisShopUser);
		if (updatePassword>0) {
			return ManageBody.getData(1, updatePassword, "");
		}else {
			return ManageBody.getData(1, updatePassword, "");
		}
	}
}
